package com.atguigu.ssyx.activity.service.impl;

import com.atguigu.client.product.ProductFeignClient;
import com.atguigu.ssyx.activity.mapper.ActivityRuleMapper;
import com.atguigu.ssyx.activity.mapper.ActivitySkuMapper;
import com.atguigu.ssyx.model.activity.ActivityInfo;
import com.atguigu.ssyx.activity.mapper.ActivityInfoMapper;
import com.atguigu.ssyx.activity.service.ActivityInfoService;
import com.atguigu.ssyx.model.activity.ActivityRule;
import com.atguigu.ssyx.model.activity.ActivitySku;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.vo.activity.ActivityRuleVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 活动表 服务实现类
 * </p>
 *
 * @author ZhangYK
 * @since 2024-07-09
 */
@Service
public class ActivityInfoServiceImpl extends ServiceImpl<ActivityInfoMapper, ActivityInfo> implements ActivityInfoService {
    @Resource
    private ProductFeignClient productFeignClient;

    @Resource
    private ActivityRuleMapper activityRuleMapper;

    @Resource
    private ActivitySkuMapper activitySkuMapper;
    /**
     * 列表方法
     * @param activityInfoPage
     * @return
     */
    @Override
    public IPage<ActivityInfo> selectPage(Page<ActivityInfo> activityInfoPage) {
        Page<ActivityInfo> infoPage = baseMapper.selectPage(activityInfoPage, null);
        List<ActivityInfo> activityInfoList = infoPage.getRecords();
        //
        activityInfoList.stream().forEach(activityInfo -> {
           activityInfo.setActivityTypeString(activityInfo.getActivityType().getComment());
        });
        return infoPage;
    }

    /**
     *根据活动id获取规则数据
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findActivityRuleList(Long id) {
        Map<String, Object> result = new HashMap<>();
        //1.根据活动id查询，查询规则列表activity_rule
        LambdaQueryWrapper<ActivityRule> queryWrapper = new LambdaQueryWrapper<ActivityRule>();
        queryWrapper.eq(ActivityRule::getActivityId, id);
        List<ActivityRule> activityRuleList = activityRuleMapper.selectList(queryWrapper);
        result.put("activityRuleList", activityRuleList);

        //2.根据活动id查询，查询使用商品id列表activity_sku,
        LambdaQueryWrapper<ActivitySku> skuLambdaQueryWrapper = new LambdaQueryWrapper<ActivitySku>();
        skuLambdaQueryWrapper.eq(ActivitySku::getActivityId, id);
        List<ActivitySku> skuList = activitySkuMapper.selectList(skuLambdaQueryWrapper);
        List<Long> skuIdList = skuList.stream().map(ActivitySku::getSkuId).collect(Collectors.toList());
        //2.1通过远程调用service-product模块接口，根据sku_id获取商品信息
        List<SkuInfo> skuInfoList = productFeignClient.findSkuInfoList(skuIdList);

        result.put("skuInfoList", skuInfoList);
        return result;
    }

    /**
     * 在活动里面添加规则
     * @param activityRuleVo
     */
    @Override
    public void saveActivityRule(ActivityRuleVo activityRuleVo) {
        //1.根据活动id删除之前的规则数据
        Long activityId = activityRuleVo.getActivityId();
        activityRuleMapper.delete(
                new LambdaQueryWrapper<ActivityRule>().eq(ActivityRule::getActivityId, activityId)
        );
        activitySkuMapper.delete(
                new LambdaQueryWrapper<ActivitySku>().eq(ActivitySku::getActivityId, activityId)
        );
        //2、获取活动规则列表数据
        List<ActivityRule> activityRuleList = activityRuleVo.getActivityRuleList();
        ActivityInfo activityInfo = baseMapper.selectById(activityId);
        for (ActivityRule activityRule : activityRuleList) {
            activityRule.setActivityId(activityId);
            activityRule.setActivityType(activityInfo.getActivityType());
            activityRuleMapper.insert(activityRule);
        }
        //3.获取规则范围数据
        List<ActivitySku> activitySkuList = activityRuleVo.getActivitySkuList();
        for (ActivitySku activitySku : activitySkuList) {
            activitySku.setActivityId(activityId);
            activitySkuMapper.insert(activitySku);
        }
    }

    /**
     * 添加活动范围
     * @param keyword
     * @return
     */
    @Override
    public List<SkuInfo> findSkuInfoByKeyword(String keyword) {
        //远程调用，根据关键字查询sku匹配内容列表
        List<SkuInfo> skuInfoByKeyword = productFeignClient.findSkuInfoByKeyword(keyword);
        //congskuinfo集合中获取所有skuid
        List<Long> skuIdList = skuInfoByKeyword.stream().map(SkuInfo::getId).collect(Collectors.toList());
        if (skuIdList.size()==0){
            return null;
        }
        //判断添加商品之前是否参加过活动,如果之前参加过不允许参加
        //1、查询两张表a-info，a-sku
        List<Long> idListExist = baseMapper.selectSkuIdListExist(skuIdList);
        //2、逻辑判断处理:排除已经参加活动的商品
        List<SkuInfo> finalSkuList = new ArrayList<>();
        for (SkuInfo skuInfo : skuInfoByKeyword) {
            if (!idListExist.contains(skuInfo.getId())){
                finalSkuList.add(skuInfo);
            }
        }
        return finalSkuList;
    }
}
